home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH6 / SRC / AALIAS3.FRM < prev    next >
Text File  |  1997-01-08  |  16KB  |  562 lines

  1. VERSION 4.00
  2. Begin VB.Form AntiAliasForm 
  3.    Caption         =   "Anti-Aliasing"
  4.    ClientHeight    =   4485
  5.    ClientLeft      =   1905
  6.    ClientTop       =   1275
  7.    ClientWidth     =   5835
  8.    DrawMode        =   14  'Copy Pen
  9.    Height          =   5175
  10.    Left            =   1845
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   299
  13.    ScaleMode       =   3  'Pixel
  14.    ScaleWidth      =   389
  15.    Top             =   645
  16.    Width           =   5955
  17.    Begin VB.CheckBox GrayCheck 
  18.       Caption         =   "Gray"
  19.       Height          =   255
  20.       Left            =   3120
  21.       TabIndex        =   9
  22.       Top             =   45
  23.       Value           =   1  'Checked
  24.       Width           =   735
  25.    End
  26.    Begin VB.CommandButton CmdGo 
  27.       Caption         =   "Go"
  28.       Default         =   -1  'True
  29.       Height          =   375
  30.       Left            =   4080
  31.       TabIndex        =   8
  32.       Top             =   0
  33.       Width           =   615
  34.    End
  35.    Begin VB.TextBox ScaleText 
  36.       Height          =   285
  37.       Left            =   2520
  38.       TabIndex        =   6
  39.       Text            =   "2"
  40.       Top             =   30
  41.       Width           =   375
  42.    End
  43.    Begin VB.PictureBox EnlargedPic 
  44.       AutoRedraw      =   -1  'True
  45.       BackColor       =   &H00C0C0C0&
  46.       ForeColor       =   &H00000000&
  47.       Height          =   3870
  48.       Left            =   1965
  49.       Picture         =   "AALIAS3.frx":0000
  50.       ScaleHeight     =   254
  51.       ScaleMode       =   3  'Pixel
  52.       ScaleWidth      =   254
  53.       TabIndex        =   4
  54.       Top             =   600
  55.       Width           =   3870
  56.    End
  57.    Begin VB.PictureBox AntiAliasedPic 
  58.       AutoRedraw      =   -1  'True
  59.       BackColor       =   &H00C0C0C0&
  60.       ForeColor       =   &H00000000&
  61.       Height          =   1935
  62.       Left            =   0
  63.       Picture         =   "AALIAS3.frx":0446
  64.       ScaleHeight     =   125
  65.       ScaleMode       =   3  'Pixel
  66.       ScaleWidth      =   125
  67.       TabIndex        =   2
  68.       Top             =   2520
  69.       Width           =   1935
  70.    End
  71.    Begin VB.PictureBox AliasedPic 
  72.       AutoRedraw      =   -1  'True
  73.       BackColor       =   &H00C0C0C0&
  74.       BeginProperty Font 
  75.          name            =   "Times New Roman"
  76.          charset         =   0
  77.          weight          =   700
  78.          size            =   15.75
  79.          underline       =   0   'False
  80.          italic          =   -1  'True
  81.          strikethrough   =   0   'False
  82.       EndProperty
  83.       ForeColor       =   &H00000000&
  84.       Height          =   1935
  85.       Left            =   0
  86.       Picture         =   "AALIAS3.frx":088C
  87.       ScaleHeight     =   125
  88.       ScaleMode       =   3  'Pixel
  89.       ScaleWidth      =   125
  90.       TabIndex        =   0
  91.       Top             =   240
  92.       Width           =   1935
  93.    End
  94.    Begin VB.Label Label1 
  95.       Caption         =   "Scale"
  96.       Height          =   255
  97.       Index           =   3
  98.       Left            =   2040
  99.       TabIndex        =   7
  100.       Top             =   45
  101.       Width           =   495
  102.    End
  103.    Begin VB.Label Label1 
  104.       Caption         =   "Enlarged"
  105.       Height          =   255
  106.       Index           =   2
  107.       Left            =   1965
  108.       TabIndex        =   5
  109.       Top             =   360
  110.       Width           =   735
  111.    End
  112.    Begin VB.Label Label1 
  113.       Caption         =   "Anti-Aliased"
  114.       Height          =   255
  115.       Index           =   1
  116.       Left            =   0
  117.       TabIndex        =   3
  118.       Top             =   2280
  119.       Width           =   975
  120.    End
  121.    Begin VB.Label Label1 
  122.       Caption         =   "Aliased"
  123.       Height          =   255
  124.       Index           =   0
  125.       Left            =   0
  126.       TabIndex        =   1
  127.       Top             =   0
  128.       Width           =   615
  129.    End
  130.    Begin VB.Menu mnuFile 
  131.       Caption         =   "&File"
  132.       Begin VB.Menu mnuFileExit 
  133.          Caption         =   "E&xit"
  134.       End
  135.    End
  136. End
  137. Attribute VB_Name = "AntiAliasForm"
  138. Attribute VB_Creatable = False
  139. Attribute VB_Exposed = False
  140. Option Explicit
  141.  
  142. Dim SysPalSize As Integer
  143. Dim NumStaticColors As Integer
  144. Dim StaticColor1 As Integer
  145. Dim StaticColor2 As Integer
  146.  
  147. Dim syspal(0 To 255) As PALETTEENTRY
  148.  
  149. ' ************************************************
  150. ' Draw some stuff to work with.
  151. ' ************************************************
  152. Sub GrayDrawStuff(pic As PictureBox)
  153. Const PI = 3.14159
  154. Const MSG = "Smile!"
  155.  
  156. Dim x1 As Single
  157. Dim x2 As Single
  158. Dim x3 As Single
  159. Dim x4 As Single
  160. Dim x5 As Single
  161. Dim x6 As Single
  162. Dim x7 As Single
  163. Dim y1 As Single
  164. Dim y2 As Single
  165. Dim dy As Single
  166. Dim r1 As Single
  167. Dim r2 As Single
  168. Dim r3 As Single
  169. Dim r4 As Single
  170.  
  171.     x1 = pic.ScaleWidth * 0.4
  172.     x2 = pic.ScaleWidth * 0.27
  173.     x3 = pic.ScaleWidth * 0.53
  174.     x4 = pic.ScaleWidth * 0.29
  175.     x5 = pic.ScaleWidth * 0.55
  176.     x6 = pic.ScaleWidth * 0.8
  177.     x7 = pic.ScaleWidth * 1
  178.     y1 = pic.ScaleHeight * 0.4
  179.     y2 = pic.ScaleHeight * 0.25
  180.     r1 = pic.ScaleHeight * 0.35
  181.     r2 = pic.ScaleHeight * 0.25
  182.     r3 = pic.ScaleHeight * 0.05
  183.     r4 = pic.ScaleHeight * 0.0375
  184.     
  185.     pic.Cls
  186.     
  187.     pic.FillStyle = vbFSSolid
  188.     pic.FillColor = RGB(&HB0, &HB0, &HB0)
  189.     pic.ForeColor = pic.FillColor
  190.     pic.Circle (x1, y1), r1
  191.     pic.FillColor = RGB(&H90, &H90, &H90)
  192.     pic.ForeColor = pic.FillColor
  193.     pic.Circle (x1, y1), r3
  194.     pic.FillColor = vbWhite
  195.     pic.ForeColor = vbBlack
  196.     pic.Circle (x2, y2), r3
  197.     pic.Circle (x3, y2), r3
  198.     pic.FillColor = vbBlack
  199.     pic.Circle (x4, y2), r4, , , , 1.5
  200.     pic.Circle (x5, y2), r4, , , , 1.5
  201.     pic.FillStyle = vbFSTransparent
  202.     pic.ForeColor = RGB(&H40, &H40, &H40)
  203.     pic.Circle (x1, y1), r2, , PI, 2 * PI
  204.     
  205.     pic.ForeColor = RGB(&H30, &H30, &H30)
  206.     pic.CurrentX = x1 - pic.TextWidth(MSG) / 2
  207.     pic.CurrentY = (pic.ScaleHeight + y1 + r1 _
  208.         - pic.TextHeight(MSG)) / 2
  209.     pic.Print MSG
  210.     
  211.     pic.ForeColor = RGB(&H50, &H50, &H50)
  212.     dy = pic.ScaleHeight / 15
  213.     For y1 = dy / 2 To pic.ScaleHeight Step dy
  214.         pic.Line (x6, y1)-(x7, y1 * 2)
  215.     Next y1
  216.  
  217.     pic.ForeColor = vbBlack
  218. End Sub
  219.  
  220. ' ************************************************
  221. ' Draw stuff in color or black and white.
  222. ' ************************************************
  223. Sub DrawIt(pic As PictureBox)
  224.     If GrayCheck.Value = vbChecked Then
  225.         GrayDrawStuff pic
  226.     Else
  227.         BWDrawStuff pic
  228.     End If
  229. End Sub
  230.  
  231.  
  232. ' ***********************************************
  233. ' Load the control's palette so the non-static
  234. ' colors are grays. Map the logical palette to
  235. ' match the system palette. Convert the image to
  236. ' use the non-static grays.
  237. '
  238. ' Leave new system palette entries in SysPal().
  239. ' ***********************************************
  240. Sub MatchGrayPalette(pic As Control)
  241. Dim origpal(0 To 255) As PALETTEENTRY
  242. Dim wid As Long
  243. Dim hgt As Long
  244. Dim bytes() As Byte
  245. Dim i As Integer
  246. Dim bm As BITMAP
  247. Dim hbm As Integer
  248. Dim status As Long
  249. Dim X As Integer
  250. Dim Y As Integer
  251. Dim gray As Single
  252. Dim dgray As Single
  253. Dim c As Integer
  254. Dim clr As Integer
  255. Dim logpal As Long
  256.  
  257.     ' Make sure pic has the foreground palette.
  258.     pic.ZOrder
  259.     status = RealizePalette(pic.hdc)
  260.     DoEvents
  261.  
  262.     ' Get the system palette entries.
  263.     status = GetSystemPaletteEntries(pic.hdc, 0, SysPalSize, origpal(0))
  264.         
  265.     ' Get the image pixels.
  266.     hbm = pic.Image
  267.     status = GetObject(hbm, BITMAP_SIZE, bm)
  268.     wid = bm.bmWidthBytes
  269.     hgt = bm.bmHeight
  270.     ReDim bytes(1 To wid, 1 To hgt)
  271.     status = GetBitmapBits(hbm, wid * hgt, bytes(1, 1))
  272.     
  273.     ' Make the logical palette as big as possible.
  274.     logpal = pic.Picture.hPal
  275.     If ResizePalette(logpal, SysPalSize) = 0 Then
  276.         Beep
  277.         MsgBox "Error resizing logical palette.", _
  278.             vbExclamation
  279.         Exit Sub
  280.     End If
  281.     
  282.     ' Blank the non-static colors.
  283.     For i = 0 To StaticColor1
  284.         syspal(i) = origpal(i)
  285.     Next i
  286.     For i = StaticColor1 + 1 To StaticColor2 - 1
  287.         With syspal(i)
  288.             .peRed = 0
  289.             .peGreen = 0
  290.             .peBlue = 0
  291.             .peFlags = PC_NOCOLLAPSE
  292.         End With
  293.     Next i
  294.     For i = StaticColor2 To 255
  295.         syspal(i) = origpal(i)
  296.     Next i
  297.     status = SetPaletteEntries(logpal, 0, SysPalSize, syspal(0))
  298.  
  299.     ' Insert the non-static grays.
  300.     gray = 0
  301.     dgray = 255 / (StaticColor2 - StaticColor1 - 2)
  302.     For i = StaticColor1 + 1 To StaticColor2 - 1
  303.         c = gray
  304.         gray = gray + dgray
  305.         With syspal(i)
  306.             .peRed = c
  307.             .peGreen = c
  308.             .peBlue = c
  309.         End With
  310.     Next i
  311.     status = SetPaletteEntries(logpal, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, syspal(StaticColor1 + 1))
  312.  
  313.     ' Realize the gray palette.
  314.     status = RealizePalette(pic.hdc)
  315.     
  316.     pic.Refresh
  317. End Sub
  318.  
  319.  
  320. ' ************************************************
  321. ' Return the index of the nonstatic gray closest
  322. ' to the given value (assuming the non-static
  323. ' colors are a gray scale created by
  324. ' MatchGrayPalette).
  325. ' ************************************************
  326. Function NearestNonstaticGray(c As Integer) As Integer
  327. Dim dgray As Single
  328.  
  329.     If c < 0 Then
  330.         c = 0
  331.     ElseIf c > 255 Then
  332.         c = 255
  333.     End If
  334.     dgray = 255 / (StaticColor2 - StaticColor1 - 2)
  335.     NearestNonstaticGray = c / dgray + StaticColor1 + 1
  336. End Function
  337.  
  338.  
  339. ' ************************************************
  340. ' Anti-alias.
  341. ' ************************************************
  342. Sub CmdGo_Click()
  343. Dim S As Integer
  344.  
  345.     MousePointer = vbHourglass
  346.     
  347.     ' Make EnlargedPic the correct size.
  348.     If Not IsNumeric(ScaleText.Text) Then _
  349.         ScaleText.Text = "2"
  350.     S = CInt(ScaleText.Text)
  351.     If S < 1 Then
  352.         ScaleText.Text = "2"
  353.         S = 2
  354.     End If
  355.     
  356.     EnlargedPic.Width = _
  357.         EnlargedPic.Width - _
  358.         EnlargedPic.ScaleWidth + _
  359.         S * AliasedPic.ScaleWidth + S
  360.     EnlargedPic.Height = _
  361.         EnlargedPic.Height - _
  362.         EnlargedPic.ScaleHeight + _
  363.         S * AliasedPic.ScaleHeight + S
  364.     
  365.     ' Make EnlargedPic use the right thicknesses.
  366.     EnlargedPic.DrawWidth = S * AliasedPic.DrawWidth
  367.     EnlargedPic.Font.Size = S * AliasedPic.Font.Size
  368.     
  369.     ' Draw the enlarged picture.
  370.     AntiAliasedPic.Cls
  371.     DrawIt EnlargedPic
  372.     DoEvents
  373.     
  374.     ' Shrink the enlarged picture.
  375.     ShrinkPicture EnlargedPic, AntiAliasedPic, S
  376.  
  377.     MousePointer = vbDefault
  378. End Sub
  379.  
  380. ' ************************************************
  381. ' Draw some stuff to work with.
  382. ' ************************************************
  383. Sub BWDrawStuff(pic As PictureBox)
  384. Const PI = 3.14159
  385. Const MSG = "Smile!"
  386.  
  387. Dim x1 As Single
  388. Dim x2 As Single
  389. Dim x3 As Single
  390. Dim x4 As Single
  391. Dim x5 As Single
  392. Dim x6 As Single
  393. Dim x7 As Single
  394. Dim y1 As Single
  395. Dim y2 As Single
  396. Dim dy As Single
  397. Dim r1 As Single
  398. Dim r2 As Single
  399. Dim r3 As Single
  400. Dim r4 As Single
  401.  
  402.     x1 = pic.ScaleWidth * 0.4
  403.     x2 = pic.ScaleWidth * 0.27
  404.     x3 = pic.ScaleWidth * 0.53
  405.     x4 = pic.ScaleWidth * 0.29
  406.     x5 = pic.ScaleWidth * 0.55
  407.     x6 = pic.ScaleWidth * 0.8
  408.     x7 = pic.ScaleWidth * 1
  409.     y1 = pic.ScaleHeight * 0.4
  410.     y2 = pic.ScaleHeight * 0.25
  411.     r1 = pic.ScaleHeight * 0.35
  412.     r2 = pic.ScaleHeight * 0.25
  413.     r3 = pic.ScaleHeight * 0.05
  414.     r4 = pic.ScaleHeight * 0.0375
  415.     
  416.     pic.Cls
  417.     
  418.     pic.Circle (x1, y1), r1
  419.     pic.Circle (x1, y1), r2, , PI, 2 * PI
  420.     pic.Circle (x1, y1), r3
  421.     pic.Circle (x2, y2), r3
  422.     pic.Circle (x3, y2), r3
  423.     pic.FillStyle = vbFSSolid
  424.     pic.Circle (x4, y2), r4, , , , 1.5
  425.     pic.Circle (x5, y2), r4, , , , 1.5
  426.     pic.FillStyle = vbFSTransparent
  427.     
  428.     pic.CurrentX = x1 - pic.TextWidth(MSG) / 2
  429.     pic.CurrentY = (pic.ScaleHeight + y1 + r1 _
  430.         - pic.TextHeight(MSG)) / 2
  431.     pic.Print MSG
  432.     
  433.     dy = pic.ScaleHeight / 15
  434.     For y1 = dy / 2 To pic.ScaleHeight Step dy
  435.         pic.Line (x6, y1)-(x7, y1 * 2)
  436.     Next y1
  437. End Sub
  438.  
  439. ' ************************************************
  440. ' Shrink fpic into tpic, reducing by a factor of
  441. ' 1/s.
  442. ' ************************************************
  443. Sub ShrinkPicture(fpic As PictureBox, tpic As PictureBox, S As Integer)
  444. Dim X As Integer
  445. Dim Y As Integer
  446. Dim i As Integer
  447. Dim j As Integer
  448. Dim clr As Long
  449. Dim status As Long
  450. Dim bm As BITMAP
  451. Dim hbm As Integer
  452. Dim wid As Long
  453. Dim hgt As Long
  454. Dim fbytes() As Byte
  455. Dim tbytes() As Byte
  456.  
  457.     ' Get the input pixels.
  458.     hbm = fpic.Image
  459.     status = GetObject(hbm, BITMAP_SIZE, bm)
  460.     wid = bm.bmWidthBytes
  461.     hgt = bm.bmHeight
  462.     ReDim fbytes(0 To wid - 1, 0 To hgt - 1)
  463.     status = GetBitmapBits(hbm, wid * hgt, fbytes(0, 0))
  464.  
  465.     ' Dimension the output pixel array.
  466.     hbm = tpic.Image
  467.     status = GetObject(hbm, BITMAP_SIZE, bm)
  468.     wid = bm.bmWidthBytes
  469.     hgt = bm.bmHeight
  470.     ReDim tbytes(0 To wid - 1, 0 To hgt - 1)
  471.  
  472.     ' Shrink the image.
  473.     For Y = 0 To hgt - 1
  474.         For X = 0 To wid - 1
  475.             ' Compute the value of pixel (x, y).
  476.             clr = 0
  477.             For i = 0 To S - 1
  478.                 For j = 0 To S - 1
  479.                     clr = clr + syspal( _
  480.                         fbytes(S * X + j, S * Y + i)).peRed
  481.                 Next j
  482.             Next i
  483.             ' Set the output pixel's value.
  484.             clr = clr / S / S
  485.             tbytes(X, Y) = NearestNonstaticGray(CInt(clr))
  486.         Next X
  487.     Next Y
  488.  
  489.     ' Update the output image.
  490.     status = SetBitmapBits(hbm, wid * hgt, tbytes(0, 0))
  491.     tpic.Refresh
  492. End Sub
  493. Private Sub Form_Load()
  494.     ' Make sure the screen supports palettes.
  495.     If Not GetDeviceCaps(hdc, RASTERCAPS) And RC_PALETTE Then
  496.         Beep
  497.         MsgBox "This monitor does not support palettes.", _
  498.             vbCritical
  499.         End
  500.     End If
  501.     
  502.     ' Get system palette size and # static colors.
  503.     SysPalSize = GetDeviceCaps(hdc, SIZEPALETTE)
  504.     NumStaticColors = GetDeviceCaps(hdc, NUMRESERVED)
  505.     StaticColor1 = NumStaticColors \ 2 - 1
  506.     StaticColor2 = SysPalSize - NumStaticColors \ 2
  507.     
  508.     ' Make the pictures all use gray palettes.
  509.     Me.Show
  510.     MousePointer = vbHourglass
  511.     DoEvents
  512.     MatchGrayPalette AliasedPic
  513.     MatchGrayPalette AntiAliasedPic
  514.     MatchGrayPalette EnlargedPic
  515.     DoEvents
  516.     
  517.     ' Blank the backgrounds.
  518.     AntiAliasedPic.Cls
  519.     EnlargedPic.Cls
  520.     
  521.     ' Make everyone use the same font.
  522.     AntiAliasedPic.Font.Name = AliasedPic.Font.Name
  523.     AntiAliasedPic.Font.Bold = AliasedPic.Font.Bold
  524.     AntiAliasedPic.Font.Italic = AliasedPic.Font.Italic
  525.     AntiAliasedPic.Font.Strikethrough = AliasedPic.Font.Strikethrough
  526.     AntiAliasedPic.Font.Underline = AliasedPic.Font.Underline
  527.  
  528.     EnlargedPic.Font.Name = AliasedPic.Font.Name
  529.     EnlargedPic.Font.Bold = AliasedPic.Font.Bold
  530.     EnlargedPic.Font.Italic = AliasedPic.Font.Italic
  531.     EnlargedPic.Font.Strikethrough = AliasedPic.Font.Strikethrough
  532.     EnlargedPic.Font.Underline = AliasedPic.Font.Underline
  533.         
  534.     ' Make AntiAliasedPic use the right thicknesses.
  535.     AntiAliasedPic.DrawWidth = AliasedPic.DrawWidth
  536.     AntiAliasedPic.Font.Size = AliasedPic.Font.Size
  537.         
  538.     ' Draw original stuff.
  539.     DrawIt AliasedPic
  540.  
  541.     MousePointer = vbDefault
  542. End Sub
  543.  
  544. Private Sub Form_Unload(Cancel As Integer)
  545.     End
  546. End Sub
  547.  
  548.  
  549. ' ************************************************
  550. ' Redraw the original stuff.
  551. ' ************************************************
  552. Private Sub GrayCheck_Click()
  553.     DrawIt AliasedPic
  554. End Sub
  555.  
  556.  
  557. Private Sub mnuFileExit_Click()
  558.     Unload Me
  559. End Sub
  560.  
  561.  
  562.